//===================================================
//Author      : GCL
//CreateTime  ：2/1/2019 12:51:52 PM
//Description ：
//===================================================
using UnityEngine;
using System.Collections;
using Mono.Data.Sqlite;
public abstract class TableReader {

	public string tableName;
	public string primeKey;
	string BaseSQLString;

	public abstract JSONObject ToJSONObject(SqliteDataReader reader);

    protected JSONObject SqliteData2Json(string sqlCommand)
    {
        JSONObject result = JSONObject.nullJO;
        SqliteDataReader reader = MetaDataDBManager.Instance.ExecuteQuery( sqlCommand );

        while( reader.Read() )
        {
            result = this.ToJSONObject( reader );
            break;
        }
        reader.Close();
        return result;
    }

    protected JSONObject SqliteData2JsonSet( string sqlCommand )
    {
        JSONObject result = new JSONObject();

        SqliteDataReader reader = MetaDataDBManager.Instance.ExecuteQuery( sqlCommand );

        while( reader.Read() )
        {
            result.Add( this.ToJSONObject( reader ) );
        }

        reader.Close();
        return result;   
    }


	public JSONObject FindByKey(int keyID)
	{
        string sqls = "SELECT * FROM "+tableName+ " WHERE " + primeKey.ToString() + "=" + keyID.ToString();

        return SqliteData2JsonSet( sqls );
	}
    /// <summary>
    /// 根据主键查找数据
    /// </summary>
    /// <param name="keyID">int类型的主键</param>
    /// <returns></returns>
    public JSONObject FindOneByOneKey(int keyID){

		string sqls = "SELECT * FROM "+tableName+ " WHERE " + primeKey.ToString() + "=" + keyID.ToString();

        return SqliteData2Json( sqls );
	}
    /// <summary>
    /// 根据主键查找数据
    /// </summary>
    /// <param name="keyID">string类型的主键</param>
    /// <returns></returns>
	public JSONObject FindOneByOneKey(string keyID){

		string sqls = "SELECT * FROM "+tableName+ " WHERE " + primeKey.ToString() + "=\"" + keyID + "\"";

        return SqliteData2Json( sqls );
	}
    /// <summary>
    /// 根据条件查找数据
    /// </summary>
    /// <param name="condition"></param>
    /// <returns></returns>
	public JSONObject FindByCondition(string condition/*"a = 5 AND b = 3"*/){
		JSONObject result = new JSONObject();
		string sqls = "SELECT * FROM "+tableName+ " WHERE " + condition;

        return SqliteData2Json( sqls );
    }
    /// <summary>
    /// 根据条件查找所有符合条件的数据
    /// </summary>
    /// <param name="condition"></param>
    /// <returns></returns>
	public JSONObject FindByAllCondition(string condition/*"a = 5 AND b = 3"*/){
		string sqls = "SELECT * FROM "+tableName+ " WHERE " + condition;

        return SqliteData2JsonSet( sqls );
    }
    /// <summary>
    /// 根据主键和一个key查找满足条件的数据
    /// </summary>
    /// <param name="keyID"></param>
    /// <returns></returns>
	public JSONObject FindByKey(string keyID)
	{
		JSONObject result = new JSONObject();
		string sqls = "SELECT * FROM "+tableName+ " WHERE " + primeKey.ToString() + "=\"" + keyID.ToString() + "\"";

        return SqliteData2JsonSet( sqls );
    }
    /// <summary>
    /// 解析字符串到int数组
    /// </summary>
    /// <param name="content"></param>
    /// <returns></returns>
    public static int[] ParseStringToIntArray(string content)
    {
        string[] arrayString = content.Split(",".ToCharArray());
		int[] result = new int[arrayString.Length];
        for (int j = 0; j < arrayString.Length; ++j)
        {
            int v;
            if (int.TryParse(arrayString[j], out v))
            {
				result[j] = v;
            }
			else
			{
				result[j] = -1;
			}
        }
		return result;
    }
    /// <summary>
    /// 解析字符串到float数组
    /// </summary>
    /// <param name="content"></param>
    /// <returns></returns>
    public static float[] ParseStringToFloatArray(string content)
    {
        string[] arrayString = content.Split(",".ToCharArray());
		float[] result = new float[arrayString.Length];
        for (int j = 0; j < arrayString.Length; ++j)
        {
            float v;
            if (float.TryParse(arrayString[j], out v))
            {
				result[j] = v;
            }
			else
			{
				result[j] = float.NaN;
			}
        }
		return result;
    }
    /// <summary>
    /// 解析字符串到bool数组
    /// </summary>
    /// <param name="content"></param>
    /// <returns></returns>
    public static bool[] ParseStringToBoolArray(string content)
    {
        string[] arrayString = content.Split(",".ToCharArray());
		bool[] result = new bool[arrayString.Length];
        for (int j = 0; j < arrayString.Length; ++j)
        {
            bool v;
            if (bool.TryParse(arrayString[j], out v))
            {
				result[j] = v;
            }
			else
			{
				result[j] = false;
			}
        }
		return result;
    }
    /// <summary>
    /// 解析字符串到字符串数组
    /// </summary>
    /// <param name="content"></param>
    /// <returns></returns>
    public static string[] ParseStringToStringArray(string content)
    {
		if (content == "" || content == null)
			return null;
        string[] arrayString = content.Split(",".ToCharArray());
		return arrayString;
    }
    /// <summary>
    /// 根据一个key查找满足条件的数据
    /// </summary>
    /// <param name="subKeyName"></param>
    /// <param name="value"></param>
    /// <returns></returns>
	public JSONObject FindByOneSubValue(string subKeyName,string value)
	{
		string sqls = "SELECT * FROM "+tableName+ " WHERE " + subKeyName + "=" + value;

        return SqliteData2JsonSet( sqls );
    }
    /// <summary>
    /// 表中所有数据
    /// </summary>
    /// <returns></returns>
    public JSONObject FindAllTable(){
	
		string sqls = "SELECT * FROM " + tableName;

        return SqliteData2JsonSet( sqls );
    }

}
